import Vue from 'vue';
import Router from 'vue-router';
import RouterLinks from "./links";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(Router);

const RouterConfig = {
  // mode: 'history',
  routes: RouterLinks
};

const router = new Router(RouterConfig);

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (localStorage.user) {  // 获取当前的token是否存在
      // console.log("token存在");
      NProgress.start()
      next();
    } else {
      next({
        path: '/login', // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else { // 如果不需要权限校验，直接进入路由界面
    next();
  }
});

router.afterEach((to, from, next) => {
  NProgress.done()
  window.scrollTo(0, 0);
});

export default router;

